/***************************************************************************
 *   This file is part of KTagger                                          *
 *   Copyright (C) 2010 by Luca Bellonda                                   *
 *   lbellonda _at_ gmail.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "ChooseWorkingProfile.h"


#include "Utility.h"
#include "ChooseProfile.h"
#include "ui_ChooseProfile.h"
#include <QInputDialog>

ChooseWorkingProfile::ChooseWorkingProfile(ktagger::KTaggerApplication *application, const QString &currentProfileName, QWidget *parent):
        QDialog(parent), ui(new Ui::ChooseProfile)
{
    _application = application;
    ui->setupUi(this);
    setModal(true);
    ui->profileName->setText(currentProfileName);
    connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

ChooseWorkingProfile::~ChooseWorkingProfile()
{

}

void ChooseWorkingProfile::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void ChooseWorkingProfile::on_btnUseAProfile_clicked()
{
    bool isOk = true;
    TagProfile* profile = chooseProfile(this, _application);
    if (NULL !=  profile) {
        if (!_application->getConfigurationManager()->saveCurrentProfile(profile)) {
            Utility::error(NULL, i18n("Error saving current profile."));
            isOk = false ;
        }
        delete profile;
        if (isOk) {
            accept();
        }
    }

    /*bool isOk = false; TODO ? delete?
    TagProfile* profile = chooseAProfile(this, isOk);
    if( !isOk ) {
      Utility::error( this, i18n("Error saving configuration") );
      return ;
    }
     if(!_application->getConfigurationmanager()->setCurrentProfile(profile)) {
        Utility::error( this, i18n("Error saving configuration") );
      return ;
     }*/
}

void ChooseWorkingProfile::on_cmdUseProfiles_clicked()
{
    if (!_application->getConfigurationManager()->saveCurrentProfile(NULL)) {
        Utility::error(NULL, i18n("Error saving current profile."));
        return ;
    }
    accept();
}


//---------------------------------------------------------------------------------------------

bool chooseWorkingProfile(QWidget *parent, const QString &currentProfileName, ktagger::KTaggerApplication *application)
{
    ChooseWorkingProfile chooseWorkingProfile(application, currentProfileName, parent);
    if (chooseWorkingProfile.exec() == QDialog::Accepted) {
        return true ;
    }
    return false ;
}
